package cn.ht.blockingQueueDemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author ht
 * @Description:
 * @date 2020-5-21
 */
public class BlockingQueueDemo {

    public static void main(String[] args) {
        //创建ArrayBlockingQueue时必须指定大小
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("A"));//true
        System.out.println(blockingQueue.add("B"));//true
        System.out.println(blockingQueue.add("C"));//true
        //添加超过队列大小的第四个元素，会抛出异常
//        System.out.println(blockingQueue.add("D"));// Queue full
        //检查排头元素(队列，先进先出)
        System.out.println(blockingQueue.element());//A
        System.out.println(blockingQueue.remove());//A
        System.out.println(blockingQueue.remove());//B
        System.out.println(blockingQueue.remove());//C
        //移除超过队列大小的第四个元素，会抛出异常
//        System.out.println(blockingQueue.remove());//java.util.NoSuchElementException
    }

}
